package com.rzl.expense.expense.service.twr.impl;

import java.math.BigDecimal;
import java.util.List;

import com.rzl.expense.common.utils.DateUtils;
import com.rzl.expense.expense.domain.twr.TwrHighRoomSummary;
import com.rzl.expense.expense.domain.twr.TwrHighTowerSummary;
import com.rzl.expense.expense.mapper.twr.TwrHighRoomSummaryMapper;
import com.rzl.expense.expense.service.twr.ITwrHighRoomSummaryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 铁塔-高价格机房占比分析汇总Service业务层处理
 *
 * @author expense
 * @date 2024-12-01
 */
@Service
public class TwrHighRoomSummaryServiceImpl implements ITwrHighRoomSummaryService {
    @Autowired
    private TwrHighRoomSummaryMapper twrHighRoomSummaryMapper;

    /**
     * 查询铁塔-高价格机房占比分析汇总
     *
     * @param staDate 铁塔-高价格机房占比分析汇总主键
     * @return 铁塔-高价格机房占比分析汇总
     */
    @Override
    public TwrHighRoomSummary selectTwrHighRoomSummaryByStaDate(String staDate) {
        return twrHighRoomSummaryMapper.selectTwrHighRoomSummaryByStaDate(staDate);
    }

    /**
     * 查询铁塔-高价格机房占比分析汇总列表
     *
     * @param twrHighRoomSummary 铁塔-高价格机房占比分析汇总
     * @return 铁塔-高价格机房占比分析汇总
     */
    @Override
    public List<TwrHighRoomSummary> selectTwrHighRoomSummaryList(TwrHighRoomSummary twrHighRoomSummary) {


        List<TwrHighRoomSummary> list = twrHighRoomSummaryMapper.selectTwrHighRoomSummaryList(twrHighRoomSummary);

        list.forEach(towerSummary -> {

            BigDecimal allCount = towerSummary.getAllCount();
            BigDecimal highRoomCount = towerSummary.getHighRoomCount();
            BigDecimal highRoomNewCount = towerSummary.getHighRoomNewCount();
            BigDecimal a = BigDecimal.ZERO;
            BigDecimal b = BigDecimal.ZERO;
            if (allCount != null && allCount.compareTo(BigDecimal.ZERO) > 0) {
                if (highRoomCount != null) {
                    a = highRoomCount.divide(allCount, 6, BigDecimal.ROUND_DOWN);
                    towerSummary.setHighRoomRatio(a);
                }
                if (highRoomNewCount != null) {
                    b = highRoomNewCount.divide(allCount, 6, BigDecimal.ROUND_DOWN);
                    towerSummary.setHighRoomNewRatio(b);
                }
            } else {
                towerSummary.setHighRoomRatio(a);
                towerSummary.setHighRoomNewRatio(b);
            }


            BigDecimal compareAllCount = towerSummary.getCompareAllCount();
            BigDecimal compareHighRoomCount = towerSummary.getCompareHighRoomCount();
            BigDecimal compareHighRoomNewCount = towerSummary.getCompareHighRoomNewCount();


            BigDecimal c = BigDecimal.ZERO;
            BigDecimal d = BigDecimal.ZERO;
            if (compareAllCount != null && compareAllCount.compareTo(BigDecimal.ZERO) > 0) {
                if (compareHighRoomCount != null) {
                    c = compareHighRoomCount.divide(compareAllCount, 6, BigDecimal.ROUND_DOWN);
                    towerSummary.setHighRoomRate(a.subtract(c));
                }
                if (compareHighRoomNewCount != null) {
                    d = compareHighRoomNewCount.divide(compareAllCount, 6, BigDecimal.ROUND_DOWN);
                    towerSummary.setHighRoomNewRate(b.subtract(d));
                }
            } else {
                towerSummary.setHighRoomRate(c);
                towerSummary.setHighRoomNewRate(d);
            }

        });
        return list;
    }

    @Override
    public List<TwrHighRoomSummary> allYearTrend(TwrHighRoomSummary twrHighRoomSummary) {

        List<TwrHighRoomSummary> list = twrHighRoomSummaryMapper.allYearTrend(twrHighRoomSummary);
        list.forEach(towerSummary -> {
            BigDecimal allCount = towerSummary.getAllCount();
            BigDecimal highRoomCount = towerSummary.getHighRoomCount();
            BigDecimal highRoomNewCount = towerSummary.getHighRoomNewCount();
            BigDecimal a = BigDecimal.ZERO;
            BigDecimal b = BigDecimal.ZERO;
            if (allCount != null && allCount.compareTo(BigDecimal.ZERO) > 0) {
                if (highRoomCount != null) {
                    a = highRoomCount.divide(allCount, 2, BigDecimal.ROUND_UP);
                    towerSummary.setHighRoomRatio(a);
                }
                if (highRoomNewCount != null) {
                    b = highRoomNewCount.divide(allCount, 2, BigDecimal.ROUND_UP);
                    towerSummary.setHighRoomNewRatio(b);
                }
            } else {
                towerSummary.setHighRoomRatio(a);
                towerSummary.setHighRoomNewRatio(b);
            }

        });
        return list;
    }

    /**
     * 新增铁塔-高价格机房占比分析汇总
     *
     * @param twrHighRoomSummary 铁塔-高价格机房占比分析汇总
     * @return 结果
     */
    @Override
    public int insertTwrHighRoomSummary(TwrHighRoomSummary twrHighRoomSummary) {
        twrHighRoomSummary.setCreateTime(DateUtils.getNowDate());
        return twrHighRoomSummaryMapper.insertTwrHighRoomSummary(twrHighRoomSummary);
    }

    /**
     * 修改铁塔-高价格机房占比分析汇总
     *
     * @param twrHighRoomSummary 铁塔-高价格机房占比分析汇总
     * @return 结果
     */
    @Override
    public int updateTwrHighRoomSummary(TwrHighRoomSummary twrHighRoomSummary) {
        return twrHighRoomSummaryMapper.updateTwrHighRoomSummary(twrHighRoomSummary);
    }

    /**
     * 批量删除铁塔-高价格机房占比分析汇总
     *
     * @param staDates 需要删除的铁塔-高价格机房占比分析汇总主键
     * @return 结果
     */
    @Override
    public int deleteTwrHighRoomSummaryByStaDates(String[] staDates) {
        return twrHighRoomSummaryMapper.deleteTwrHighRoomSummaryByStaDates(staDates);
    }

    /**
     * 删除铁塔-高价格机房占比分析汇总信息
     *
     * @param staDate 铁塔-高价格机房占比分析汇总主键
     * @return 结果
     */
    @Override
    public int deleteTwrHighRoomSummaryByStaDate(String staDate) {
        return twrHighRoomSummaryMapper.deleteTwrHighRoomSummaryByStaDate(staDate);
    }


    @Override
    public void batchTwrHighRoomSummary(List<TwrHighRoomSummary> list) {

        twrHighRoomSummaryMapper.batchTwrHighRoomSummary(list);
    }

    @Override
    public List<String> summaryStaDates() {
        return twrHighRoomSummaryMapper.summaryStaDates();
    }

    @Override
    public List<TwrHighRoomSummary> summaryHighRoomByStaDate(String date) {
        return twrHighRoomSummaryMapper.summaryHighRoomByStaDate(date);
    }

    @Override
    public TwrHighRoomSummary summaryCompareHighRoom(TwrHighRoomSummary twrHighRoomSummary) {
        return twrHighRoomSummaryMapper.summaryCompareHighRoom(twrHighRoomSummary);
    }

}
